<template>
  <div class="app-container">
    <a-form :model="queryParams" ref="queryRef" layout="inline" v-show="showSearch">
      <a-form-item label="设备名称" name="deviceName">
        <a-input
            v-model:value="queryParams.deviceName"
            placeholder="请输入设备名称"
            allow-clear
            @pressEnter="handleQuery"
        />
      </a-form-item>
      <a-form-item label="编码设备" name="deviceCode">
        <a-input
            v-model:value="queryParams.deviceCode"
            placeholder="请输入编码设备"
            allow-clear
            @pressEnter="handleQuery"
        />
      </a-form-item>
      <a-form-item label="设备类型" name="deviceLeixing">
        <a-input
            v-model:value="queryParams.deviceLeixing"
            placeholder="请输入设备类型"
            allow-clear
            @pressEnter="handleQuery"
        />
      </a-form-item>
      <a-form-item label="时间" name="time">
        <a-date-picker 
                        v-model:value="queryParams.time"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择时间">
        </a-date-picker>
      </a-form-item>
      <a-form-item>
        <a-button type="primary" @click="handleQuery">
          <template #icon><SearchOutlined /></template>
          搜索
        </a-button>
        <a-button @click="resetQuery" style="margin-left: 8px">
          <template #icon><ReloadOutlined /></template>
          重置
        </a-button>
      </a-form-item>
    </a-form>

    <a-row :gutter="10" class="mb8">
      <a-col :span="1.5">
        <a-button
            type="primary"
            @click="handleAdd"
            v-privilege="['DeviceSys:deviceInfo:add']"
        >
          <template #icon><PlusOutlined /></template>
          新增
        </a-button>
      </a-col>
      <a-col :span="1.5">
        <a-button
            type="default"
            :disabled="single"
            @click="handleUpdate"
            v-privilege="['DeviceSys:deviceInfo:edit']"
        >
          <template #icon><EditOutlined /></template>
          修改
        </a-button>
      </a-col>
      <a-col :span="1.5">
        <a-button
            type="primary"
            danger
            :disabled="multiple"
            @click="handleDelete"
            v-privilege="['DeviceSys:deviceInfo:remove']"
        >
          <template #icon><DeleteOutlined /></template>
          删除
        </a-button>
      </a-col>
      <a-col :span="1.5">
        <a-button
            type="default"
            @click="handleExport"
            v-privilege="['DeviceSys:deviceInfo:export']"
        >
          <template #icon><DownloadOutlined /></template>
          导出
        </a-button>
      </a-col>
<!--      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>-->
    </a-row>

    <a-table 
      :loading="loading" 
      :data-source="deviceInfoList" 
      :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: handleSelectionChange }"
      row-key="deviceName"
    >
      <a-table-column title="设备名称" data-index="deviceName" align="center"/>
      <a-table-column title="编码设备" data-index="deviceCode" align="center"/>
      <a-table-column title="设备类型" data-index="deviceLeixing" align="center"/>
      <a-table-column title="时间" data-index="time" align="center" :width="180">
        <template #default="{ record }">
          <span>{{ parseTime(record.time, '{y}-{m}-{d}') }}</span>
        </template>
      </a-table-column>
      <a-table-column title="备注" data-index="remark" align="center"/>
      <a-table-column title="操作" align="center" class-name="small-padding fixed-width">
        <template #default="{ record }">
          <a-space>
            <a-button type="link" @click="handleUpdate(record)"
                     v-privilege="['DeviceSys:deviceInfo:edit']">
              <template #icon><EditOutlined /></template>
              修改
            </a-button>
            <a-button type="link" danger @click="handleDelete(record)"
                     v-privilege="['DeviceSys:deviceInfo:remove']">
              <template #icon><DeleteOutlined /></template>
              删除
            </a-button>
          </a-space>
        </template>
      </a-table-column>
    </a-table>

    <div class="table-pagination" style="margin-top: 16px; text-align: right" v-show="total > 0">
      <a-pagination
          :total="total"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          @change="handlePageChange"
          @showSizeChange="handlePageSizeChange"
          show-size-changer
          :show-total="(total) => `共 ${total} 条`"
      />
    </div>

    <!-- 添加或修改车载设备信息对话框 -->
    <a-modal :title="title" v-model:open="open" width="500px">
      <a-form ref="deviceInfoRef" :model="form" :rules="rules" :label-col="{ span: 6 }" :wrapper-col="{ span: 18 }">
        <a-form-item label="设备名称" name="deviceName">
          <a-input v-model:value="form.deviceName" placeholder="请输入设备名称"/>
        </a-form-item>
        <a-form-item label="编码设备" name="deviceCode">
          <a-input v-model:value="form.deviceCode" placeholder="请输入编码设备"/>
        </a-form-item>
        <a-form-item label="设备类型" name="deviceLeixing">
          <a-input v-model:value="form.deviceLeixing" placeholder="请输入设备类型"/>
        </a-form-item>
        <a-form-item label="时间" name="time">
          <a-date-picker 
                          v-model:value="form.time"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择时间">
          </a-date-picker>
        </a-form-item>
        <a-form-item label="备注" name="remark">
          <a-input v-model:value="form.remark" placeholder="请输入备注"/>
        </a-form-item>
      </a-form>
      <template #footer>
        <a-button type="primary" @click="submitForm">确 定</a-button>
        <a-button @click="cancel">取 消</a-button>
      </template>
    </a-modal>
  </div>
</template>

<script setup name="DeviceInfo">
import {ref,getCurrentInstance, reactive, toRefs} from 'vue';
import {
  listDeviceInfo,
  getDeviceInfo,
  delDeviceInfo,
  addDeviceInfo,
  updateDeviceInfo
} from "/@/api/offroad/DeviceSys/deviceInfo";
import { 
  SearchOutlined,
  ReloadOutlined,
  PlusOutlined,
  EditOutlined,
  DeleteOutlined,
  DownloadOutlined
} from '@ant-design/icons-vue';

// 日期处理函数
const parseTime = (time, format) => {
  if (!time) return '';
  const date = new Date(time);
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  return format.replace('{y}', year).replace('{m}', month).replace('{d}', day);
};

const {proxy} = getCurrentInstance();
const deviceInfoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const selectedRowKeys = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    deviceName: null,
    deviceCode: null,
    deviceLeixing: null,
    time: null,
  },
  rules: {
    deviceName: [
      {required: true, message: "设备名称不能为空", trigger: "blur"}
    ],
    deviceCode: [
      {required: true, message: "编码设备不能为空", trigger: "blur"}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询车载设备信息列表 */
function getList() {
  loading.value = true;
  listDeviceInfo(queryParams.value).then(response => {
    deviceInfoList.value = response.data.list;
    total.value = response.data.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    deviceName: null,
    deviceCode: null,
    deviceLeixing: null,
    time: null,
    remark: null
  };
  proxy.resetForm("deviceInfoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selectedKeys, selectedRows) {
  selectedRowKeys.value = selectedKeys;
  ids.value = selectedRows.map(item => item.deviceName);
  single.value = selectedRows.length != 1;
  multiple.value = !selectedRows.length;
}

// 分页页码变化
function handlePageChange(page, pageSize) {
  queryParams.value.pageNum = page;
  queryParams.value.pageSize = pageSize;
  getList();
}

// 分页大小变化
function handlePageSizeChange(current, size) {
  queryParams.value.pageNum = 1;
  queryParams.value.pageSize = size;
  getList();
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加车载设备信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _deviceName = row.deviceName || ids.value
  getDeviceInfo(_deviceName).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改车载设备信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["deviceInfoRef"].validate().then(() => {
    if (form.value.deviceName != null) {
      updateDeviceInfo(form.value).then(response => {
        proxy.$message.success("修改成功");
        open.value = false;
        getList();
      });
    } else {
      addDeviceInfo(form.value).then(response => {
        proxy.$message.success("新增成功");
        open.value = false;
        getList();
      });
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _deviceNames = row.deviceName || ids.value;
  proxy.$modal.confirm({
    title: '确认删除',
    content: '是否确认删除车载设备信息编号为"' + _deviceNames + '"的数据项？',
    onOk() {
      return delDeviceInfo(_deviceNames).then(() => {
        getList();
        proxy.$message.success("删除成功");
      });
    }
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('DeviceSys/deviceInfo/export', {
    ...queryParams.value
  }, `deviceInfo_${new Date().getTime()}.xlsx`)
}

getList();
</script>